package com.homejoy.system.service.impl;

import java.util.List;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.homejoy.common.utils.DateUtils;
import com.homejoy.common.utils.SecurityUtils;
import com.homejoy.common.utils.StringUtils;
import com.homejoy.common.utils.uuid.IdUtils;
import com.homejoy.system.domain.HomejoyPropertyAudit;
import com.homejoy.system.domain.HomejoyPropertyImage;
import com.homejoy.system.domain.vo.HomeJoyPropertyVo;
import com.homejoy.system.mapper.HomejoyPropertyAuditMapper;
import com.homejoy.system.mapper.HomejoyPropertyImageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.homejoy.system.mapper.HomejoyPropertyMapper;
import com.homejoy.system.domain.HomejoyProperty;
import com.homejoy.system.service.IHomejoyPropertyService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 房源基础信息Service业务层处理
 *
 * @author homeJoy
 * @date 2025-11-08
 */
@Service
public class HomejoyPropertyServiceImpl implements IHomejoyPropertyService {

    @Resource
    private HomejoyPropertyMapper homejoyPropertyMapper;

    @Resource
    private HomejoyPropertyAuditMapper homejoyPropertyAuditMapper;

    @Resource
    private HomejoyPropertyImageMapper homejoyPropertyImageMapper;



    @Override
    public List<HomeJoyPropertyVo> selectHomeJoyPropertyVoList(HomejoyProperty homejoyProperty) {
        List<HomeJoyPropertyVo> homeJoyPropertyVos = homejoyPropertyMapper.selectHomeJoyPropertyVoList(homejoyProperty);
        homeJoyPropertyVos.forEach(c->{
            c.setImageList(homejoyPropertyImageMapper.selectHomeJoyInfoByPropertyId(Long.valueOf(c.getPropertyId())));
            HomejoyPropertyAudit audit = homejoyPropertyAuditMapper.selectHomejoyPropertyAuditByPid(Long.valueOf(c.getPropertyId()));
            c.setAuditStatus(audit.getAuditStatus());
        });
        return homeJoyPropertyVos;
    }

    /**
     * 查询房源基础信息
     * @param propertyId 房源基础信息主键
     * @return 房源基础信息
     */
    @Override
    public HomejoyProperty selectHomejoyPropertyByPropertyId(Long propertyId) {
        HomejoyProperty property = homejoyPropertyMapper.selectHomejoyPropertyByPropertyId(propertyId);
        List<HomejoyPropertyImage> imageList = homejoyPropertyImageMapper.selectHomeJoyInfoByPropertyId(propertyId);
        property.setImageList(imageList);
        return property;
    }

    /**
     * 查询房源基础信息列表
     *
     * @param homejoyProperty 房源基础信息
     * @return 房源基础信息
     */
    @Override
    public List<HomejoyProperty> selectHomejoyPropertyList(HomejoyProperty homejoyProperty) {
        return homejoyPropertyMapper.selectHomejoyPropertyList(homejoyProperty);
    }

    /**
     * 新增房源基础信息
     *
     * @param homejoyProperty 房源基础信息
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertHomejoyProperty(HomejoyProperty homejoyProperty) {
        //1. 填入批次号之类的东西
        homejoyProperty.setPropertyNo(IdUtil.getSnowflake().nextIdStr());
        homejoyProperty.setCreateTime(DateUtils.getNowDate());
        //创建者为出租者自身
        String username = SecurityUtils.getUsername();
        homejoyProperty.setCreateBy(username);
        //待审核
        homejoyProperty.setPublishStatus(2);
        Long userId = SecurityUtils.getUserId();
        homejoyProperty.setLandlordId(userId);
        homejoyProperty.setLandlordName(username);
        homejoyProperty.setDisplayStatus(0);
        int add = homejoyPropertyMapper.insertHomejoyProperty(homejoyProperty);
        //2. 把数据添加到audit表里进行审核，赋予状态
        HomejoyPropertyAudit audit = new HomejoyPropertyAudit();
        audit.setPropertyId(homejoyProperty.getPropertyId());
        audit.setCreateBy(username);
        audit.setCreateTime(DateUtils.getNowDate());
        //3.把图片插入图片表
        if (ObjectUtil.isNotEmpty(homejoyProperty)) {
            if (CollUtil.isNotEmpty(homejoyProperty.getImageList())) {
                //3.1封面只有一张
                //3.2其他图集
                homejoyProperty.getImageList().forEach(c -> {
                    c.setPropertyId(homejoyProperty.getPropertyId());
                    c.setCreateBy(username);
                    c.setCreateTime(DateUtils.getNowDate());
                    homejoyPropertyImageMapper.insertHomejoyPropertyImage(c);
                });
            }
        }
        //审核中
        audit.setAuditStatus(3);
        audit.setAuditorId(userId);
        audit.setAuditorName(username);
        homejoyPropertyAuditMapper.insertHomejoyPropertyAudit(audit);
        return add;
    }

    /**
     * 修改房源基础信息
     *
     * @param homejoyProperty 房源基础信息
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateHomejoyProperty(HomejoyProperty homejoyProperty) {
        homejoyProperty.setUpdateTime(DateUtils.getNowDate());
        String username = SecurityUtils.getUsername();
        //校验，只有出租者自己创建的数据可以修改
        boolean admin = SecurityUtils.isAdmin(SecurityUtils.getUserId());
        if(!admin){
            HomejoyProperty property = homejoyPropertyMapper.selectHomejoyPropertyByPropertyId(homejoyProperty.getPropertyId());
            if (!property.getCreateBy().equals(username)) {
                throw new RuntimeException("请勿修改其他出租者信息");
            }
        }
        homejoyProperty.setUpdateBy(username);
        homejoyProperty.setVersion(homejoyProperty.getVersion()+1);
        homejoyProperty.setDisplayStatus(0);
        int i = homejoyPropertyMapper.updateHomejoyProperty(homejoyProperty);
        if (ObjectUtil.isNotEmpty(homejoyProperty)) {
            if (CollUtil.isNotEmpty(homejoyProperty.getImageList())) {
                homejoyProperty.getImageList().forEach(c -> {
                    c.setPropertyId(homejoyProperty.getPropertyId());
                    //查一下
                    int count = homejoyPropertyImageMapper.countSelectImageByInfo(c);
                    if(count>0){
                        c.setUpdateBy(username);
                        c.setUpdateTime(DateUtils.getNowDate());
                        homejoyPropertyImageMapper.updateHomeJoyPropertyImageInfo(c);
                    }else{
                        c.setCreateBy(username);
                        c.setCreateTime(DateUtils.getNowDate());
                        homejoyPropertyImageMapper.insertHomejoyPropertyImage(c);
                    }
                });
            }
        }
        HomejoyPropertyAudit audit = new HomejoyPropertyAudit();
        audit.setPropertyId(homejoyProperty.getPropertyId());
        audit.setCreateBy(username);
        audit.setCreateTime(DateUtils.getNowDate());
        audit.setAuditStatus(3);
        audit.setAuditorId(SecurityUtils.getUserId());
        audit.setAuditorName(username);
        homejoyPropertyAuditMapper.updateHomejoyPropertyAuditByPropertyId(audit);
        return i;
    }

    /**
     * 批量删除房源基础信息
     *
     * @param propertyIds 需要删除的房源基础信息主键
     * @return 结果
     */
    @Override
    public int deleteHomejoyPropertyByPropertyIds(Long[] propertyIds) {
        return homejoyPropertyMapper.deleteHomejoyPropertyByPropertyIds(propertyIds);
    }

    /**
     * 删除房源基础信息信息
     *
     * @param propertyId 房源基础信息主键
     * @return 结果
     */
    @Override
    public int deleteHomejoyPropertyByPropertyId(Long propertyId) {
        return homejoyPropertyMapper.deleteHomejoyPropertyByPropertyId(propertyId);
    }
}
